// const autojsUtils = require('./modules/autojs-utils');
auto();

device.keepScreenOn();

const MomoGhsAppOpen = require("./momoGhsAppOpen.js");
const MomoGhsPageUtil = require("./momoGhsPageUtil.js");
const MomoGhsDoLogin = require("./momoGhsDoLogin.js");
const MomoGhsDoTransfer = require("./momoGhsDoTransfer.js");
const MomoGhsFetchBills = require("./momoGhsFetchBills.js");
const SmsUtil = require("./baseSmsUtil.js");
const ApiUrl = require("./baseApiUrl.js");
const HttpUtil = require("./baseHttpUtil.js");
const Constant = require("./constant.js");
const RunDefine = require("./baseRunDefine.js");

// 程序结束自动 隐藏控制台
console.show(false);
console.setMaxLines(15);

// 转账信息
let targetMtnTransferAmount = "";
let targetMtnTransferId = "";
let targetMtnTransferAccount = "";
let targetMtnTransferReference = "";

// 是否需要重启app
let isNeedRestartMomo = false;

// 是否需要重启脚本
let isNeedRestartScript = false;

// 测试转账
// let testMtnTransfer = true;

const initMtnTransfer = (data) => {
    targetMtnTransferAmount = data.amount + "";
    targetMtnTransferId = data.id + "";
    targetMtnTransferAccount = data.mtn + "";
    targetMtnTransferReference = data.reference + "";
}

const isValidMtnTransfer = () => {
    let result1 = targetMtnTransferAmount || "";
    let result2 = targetMtnTransferId || "";
    let result3 = targetMtnTransferAccount || "";

    return ((result1.length > 0) && (result2.length > 0) && (result3.length > 0));
}

const cleanMtnTransfer = () => {
    targetMtnTransferAmount = "";
    targetMtnTransferId = "";
    targetMtnTransferAccount = "";
    targetMtnTransferReference = "";
}

const printMtnTransferLog = () => {
    console.log(Constant.UI_SDK_VERSION + "targetMtnTransferAmount: " + targetMtnTransferAmount + ",\ntargetMtnTransferId: " + targetMtnTransferId + ",\ntargetMtnTransferAccount: " + targetMtnTransferAccount + ",\ntargetMtnTransferReference: " + targetMtnTransferReference);
}

// 上传短信
SmsUtil.syncSmsData();

console.log(Constant.UI_SDK_VERSION + "当前设备: " + Constant.getMtn());
sleep(5 * 1000);
MomoGhsAppOpen.openMomoApp();

// 上传短信
SmsUtil.syncSmsData();
sleep(5 * 1000);

// login com.comviva.webaxn.ui.WebAxnActivity
// pin com.comviva.webaxn.ui.WebAxnActivity
// com.comviva.webaxn.ui.WebAxnActivity
// com.comviva.webaxn.ui.WebAxnActivity
// com.comviva.webaxn.ui.WebAxnActivity
// com.comviva.webaxn.ui.WebAxnActivity

// android.app.Dialog
// android.view.View



while (true) {
    // 清空转账信息
    cleanMtnTransfer();

    // 页面状态
    let isLoginPage = false;
    let isPinPage = false;
    let isHomePage = false;
    let isSendMoneyPage = false;
    let isMtnMoMoAccountInputPage = false;
    let isMtnMoMoAccountConfirmPage = false;
    let isMtnMoMoTransferSuccessPage = false;
    let isMtnMoMoTransferFailPage = false;
    let isBillSeeAllPage = false;
    let isBillListPage = false;

    // 初始化页面标识
    isLoginPage = MomoGhsPageUtil.isLoginPage();
    isPinPage = MomoGhsPageUtil.isLoginPinPage();
    isHomePage = MomoGhsPageUtil.isHomePage();
    isBillSeeAllPage = MomoGhsPageUtil.isSelectAllPage();
    isBillListPage = MomoGhsPageUtil.isTransactionListPage();

    if (isLoginPage) {
        // 置空其它标识
        isLoginPage = true;
        isPinPage = false;
        isHomePage = false;
        isSendMoneyPage = false;
        isMtnMoMoAccountInputPage = false;
        isMtnMoMoAccountConfirmPage = false;
        isMtnMoMoTransferSuccessPage = false;
        isMtnMoMoTransferFailPage = false;
        isBillSeeAllPage = false;
        isBillListPage = false;

    } else if (isPinPage) {
        // 置空其它标识
        isLoginPage = false;
        isPinPage = true;
        isHomePage = false;
        isSendMoneyPage = false;
        isMtnMoMoAccountInputPage = false;
        isMtnMoMoAccountConfirmPage = false;
        isMtnMoMoTransferSuccessPage = false;
        isMtnMoMoTransferFailPage = false;
        isBillSeeAllPage = false;
        isBillListPage = false;

    } else if (isHomePage) {
        // 置空其它标识
        isLoginPage = false;
        isPinPage = false;
        isHomePage = true;
        isSendMoneyPage = false;
        isMtnMoMoAccountInputPage = false;
        isMtnMoMoAccountConfirmPage = false;
        isMtnMoMoTransferSuccessPage = false;
        isMtnMoMoTransferFailPage = false;
        isBillSeeAllPage = false;
        isBillListPage = false;

    } else if (isBillSeeAllPage) {
        // 置空其它标识
        isLoginPage = false;
        isPinPage = false;
        isHomePage = false;
        isSendMoneyPage = false;
        isMtnMoMoAccountInputPage = false;
        isMtnMoMoAccountConfirmPage = false;
        isMtnMoMoTransferSuccessPage = false;
        isMtnMoMoTransferFailPage = false;
        isBillSeeAllPage = true;
        isBillListPage = false;

    } else if (isBillListPage) {
        // 置空其它标识
        isLoginPage = false;
        isPinPage = false;
        isHomePage = false;
        isSendMoneyPage = false;
        isMtnMoMoAccountInputPage = false;
        isMtnMoMoAccountConfirmPage = false;
        isMtnMoMoTransferSuccessPage = false;
        isMtnMoMoTransferFailPage = false;
        isBillSeeAllPage = false;
        isBillListPage = true;
    }

    // 判断是否在 momo 应用内
    let isInMomoApp = true;
    //MomoGhsPageUtil.isInMomoApp();
    if (!isInMomoApp) {
        sleep(1 * 1000);
        home();
        sleep(2 * 1000);
        MomoGhsAppOpen.openMomoApp();

        // 上传短信
        SmsUtil.syncSmsData();
        sleep(1 * 1000);

        // 清空控制台
        console.clear();
        continue;
    }

    // 上传短信
    SmsUtil.syncSmsData();
    sleep(1 * 1000);

    // 获取脚本状态信息
    let url = ApiUrl.checkOperStatus();
    let checkOperStatusParams = { phone: Constant.getMtn() }
    let checkOperStatusResult = HttpUtil.sendPostSync(url, checkOperStatusParams)
    if (checkOperStatusResult !== null) {
        isNeedRestartMomo = checkOperStatusResult.momo || false;
        isNeedRestartScript = checkOperStatusResult.script || false;
    } else {
        isNeedRestartMomo = false;
        isNeedRestartScript = false;
    }

    if (isNeedRestartScript) {
        sleep(1 * 1000);
        // 清空控制台
        console.clear();
        continue;
    }

    // 判断是否要重启app
    if (isNeedRestartMomo) {
        // 走关闭流程
        MomoGhsAppOpen.closeMomoApp();

        // 走重启流程
        MomoGhsAppOpen.openMomoApp();

        // 清空控制台
        console.clear();
        continue;

    } else {

        // 判断是否为登录-手机号页
        if (isLoginPage) {
            // 清空控制台
            console.clear();

            // 走登录-手机号流程
            let doLoginResult = MomoGhsDoLogin.doLogin();
            if (doLoginResult) {
                // 循环判断
                // 判断是否为 pin 页
                for (let pinIndex = 0; pinIndex < RunDefine.loopQueryCount; pinIndex++) {

                    // 计算公式： (n+1)%10 = 0 时上报短信
                    if (((pinIndex + 1) % 10) === 0) {
                        // 上传短信
                        SmsUtil.syncSmsData();
                    }

                    let pinQueryResult = MomoGhsPageUtil.isLoginPinPage();
                    if (pinQueryResult) {
                        isPinPage = true;
                        break;
                    }
                    sleep(1 * 1000);
                }
            }
        }

        // 判断是否为登录-PIN页
        if (isPinPage) {
            // 清空控制台
            console.clear();

            // 走登录-PIN流程
            let doPinResult = MomoGhsDoLogin.doPin();
            if (doPinResult) {
                // 循环判断
                // 判断是否为 首页
                for (let homeIndex = 0; homeIndex < RunDefine.loopQueryCount; homeIndex++) {

                    // 计算公式： (n+1)%10 = 0 时上报短信
                    if (((homeIndex + 1) % 10) === 0) {
                        // 上传短信
                        SmsUtil.syncSmsData();
                    }

                    let homeQueryResult = MomoGhsPageUtil.isHomePage();
                    if (homeQueryResult) {
                        isHomePage = true;
                        break;
                    }
                    sleep(1 * 1000);
                }
            }
        }

        // 判断是否为首页
        if (isHomePage) {
            // 清空控制台
            console.clear();

            // 获取转账信息
            cleanMtnTransfer();
            let url = ApiUrl.getMtnTransferAccount();
            let mtnAccountParams = { num: "1", phone: Constant.getMtn() }
            let mtnAccountResult = HttpUtil.sendPostSync(url, mtnAccountParams)
            if (mtnAccountResult !== null) {
                if (mtnAccountResult.length > 0) {
                    initMtnTransfer(mtnAccountResult[0]);
                } else {
                    cleanMtnTransfer();
                }
            } else {
                cleanMtnTransfer();
            }

            // 清空控制台
            console.clear();

            // 打印转账信息
            printMtnTransferLog();
        }

        // 判断是否需要转账
        if (isValidMtnTransfer()) {
            // 走打开 Send Money 流程
            let doOpenSendMoneyPageResult = MomoGhsDoTransfer.doOpenSendMoneyPage();
            if (doOpenSendMoneyPageResult) {
                // 循环判断
                // 判断是否为 Send Money 页
                for (let sendMoneyIndex = 0; sendMoneyIndex < RunDefine.loopQueryCount; sendMoneyIndex++) {

                    // 计算公式： (n+1)%10 = 0 时上报短信
                    if (((sendMoneyIndex + 1) % 10) === 0) {
                        // 上传短信
                        SmsUtil.syncSmsData();
                    }

                    let sendMoneyQueryResult = MomoGhsPageUtil.isSendMoneyMtnPage();
                    if (sendMoneyQueryResult) {
                        isSendMoneyPage = true;
                        isHomePage = false;
                        break;
                    }
                    sleep(1 * 1000);
                }
            }
        }

        // 判断是否是 Send Money 页
        if (isSendMoneyPage && isValidMtnTransfer()) {
            // 清空控制台
            console.clear();

            // 走打开 To MTN MoMo 流程
            let doOpenMtnMoMoPageResult = MomoGhsDoTransfer.doOpenMtnMoMoPage();
            if (doOpenMtnMoMoPageResult) {
                // 循环判断
                // 判断是否为 To MTN MoMo 页
                for (let mtnMoMoIndex = 0; mtnMoMoIndex < RunDefine.loopQueryCount; mtnMoMoIndex++) {

                    // 计算公式： (n+1)%10 = 0 时上报短信
                    if (((mtnMoMoIndex + 1) % 10) === 0) {
                        // 上传短信
                        SmsUtil.syncSmsData();
                    }

                    let mtnMoMoAccountInputQueryResult = MomoGhsPageUtil.isSendMoneyMtnAccountInputPage();
                    if (mtnMoMoAccountInputQueryResult) {
                        isMtnMoMoAccountInputPage = true;
                        isHomePage = false;
                        break;
                    }
                    sleep(1 * 1000);
                }
            }
        }

        // 判断是否为转账 Mtn account input 页
        if (isMtnMoMoAccountInputPage && isValidMtnTransfer()) {
            // 清空控制台
            console.clear();

            // 开始转账流程
            let doMtnMoMoAccountInputResult = MomoGhsDoTransfer.doMtnMoMoAccountInput(targetMtnTransferAccount, targetMtnTransferAmount, targetMtnTransferReference);
            if (doMtnMoMoAccountInputResult) {
                // 循环判断
                // 判断是否为 Mtn MoMo account confirm 页
                for (let mtnMoMoAccountInputIndex = 0; mtnMoMoAccountInputIndex < RunDefine.loopQueryCount; mtnMoMoAccountInputIndex++) {

                    // 计算公式： (n+1)%10 = 0 时上报短信
                    if (((mtnMoMoAccountInputIndex + 1) % 10) === 0) {
                        // 上传短信
                        SmsUtil.syncSmsData();
                    }

                    let mtnMoMoAccountConfirmQueryResult = MomoGhsPageUtil.isSendMoneyMtnAccountConfirmPage();
                    if (mtnMoMoAccountConfirmQueryResult) {
                        isMtnMoMoAccountConfirmPage = true;
                        isHomePage = false;
                        break;
                    }
                    sleep(1 * 1000);
                }
            }
        }

        // 判断是否为转账 Mtn account confirm 页
        if (isMtnMoMoAccountConfirmPage) {
            // 清空控制台
            console.clear();

            // 点击确认
            let doClickTransferComletedResult = MomoGhsDoTransfer.doClickTransferComleted();
            if (doClickTransferComletedResult) {
                // 循环判断
                // 判断是否为 Mtn MoMo transfer success/fail 页
                for (let mtnMoMoTransferCompleteIndex = 0; mtnMoMoTransferCompleteIndex < RunDefine.loopQueryCount; mtnMoMoTransferCompleteIndex++) {

                    // 计算公式： (n+1)%10 = 0 时上报短信
                    if (((mtnMoMoTransferCompleteIndex + 1) % 10) === 0) {
                        // 上传短信
                        SmsUtil.syncSmsData();
                    }

                    // success
                    let mtnMoMoTransferSuccessQueryResult = MomoGhsPageUtil.isSendMoneyMtnSuccessPage();
                    if (mtnMoMoTransferSuccessQueryResult) {
                        isMtnMoMoTransferSuccessPage = true;
                        isMtnMoMoTransferFailPage = false;
                        isHomePage = false;
                        break;
                    }

                    // fail
                    let mtnMoMoTransferFailQueryResult = MomoGhsPageUtil.isSendMoneyMtnFailPage();
                    if (mtnMoMoTransferFailQueryResult) {
                        isMtnMoMoTransferSuccessPage = false;
                        isMtnMoMoTransferFailPage = true;
                        isHomePage = false;
                        break;
                    }

                    sleep(1 * 1000);
                }
            }
        }

        // 判断是否为转账 Mtn momo transfer success 页
        if (isMtnMoMoTransferSuccessPage) {
            // 清空控制台
            console.clear();

            isMtnMoMoTransferFailPage = false;
            isHomePage = false;

            // 保存转账成功
            let url = ApiUrl.saveMtnTransferResult();
            let mtnTransferResultParams = { id: targetMtnTransferId, response: "", status: "2", txnId: "" }
            HttpUtil.sendPostSync(url, mtnTransferResultParams)
            sleep(1 * 1000);

            // 清空转账信息
            cleanMtnTransfer();

            // 完成转账流程，返回 首页
            let doClickTransferDoneAndGoBackResult = MomoGhsDoTransfer.doClickTransferDoneAndGoBack();
            if (doClickTransferDoneAndGoBackResult) {
                // 循环判断
                // 判断是否为 Send Money 页
                for (let clickTransferDoneAndGoBackIndex = 0; clickTransferDoneAndGoBackIndex < RunDefine.loopQueryCount; clickTransferDoneAndGoBackIndex++) {

                    // 计算公式： (n+1)%10 = 0 时上报短信
                    if (((clickTransferDoneAndGoBackIndex + 1) % 10) === 0) {
                        // 上传短信
                        SmsUtil.syncSmsData();
                    }

                    let sendMoneyQueryResult = MomoGhsPageUtil.isSendMoneyMtnPage();
                    if (sendMoneyQueryResult) {
                        // 返回
                        back();
                        sleep(1 * 1000);
                        break;
                    }
                    sleep(1 * 1000);
                }
            }
        }

        // 判断是否为转账 Mtn momo transfer fail 页
        if (isMtnMoMoTransferFailPage) {
            // 清空控制台
            console.clear();

            isMtnMoMoTransferSuccessPage = false;
            isHomePage = false;

            // 保存转账失败
            let url = ApiUrl.saveMtnTransferResult();
            let mtnTransferResultParams = { id: targetMtnTransferId, response: "", status: "3", txnId: "" }
            HttpUtil.sendPostSync(url, mtnTransferResultParams)
            sleep(1 * 1000);

            // 清空转账信息
            cleanMtnTransfer();

            // 完成转账流程，需要重启
        }

        // 循环判断
        // 判断是否为 首页
        for (let homeIndex = 0; homeIndex < RunDefine.loopQueryCount; homeIndex++) {

            // 计算公式： (n+1)%10 = 0 时上报短信
            if (((homeIndex + 1) % 10) === 0) {
                // 上传短信
                SmsUtil.syncSmsData();
            }

            let homeQueryResult = MomoGhsPageUtil.isHomePage();
            if (homeQueryResult) {
                isHomePage = true;
                break;
            }
            sleep(1 * 1000);
        }

        // 判断是否为首页
        if (isHomePage) {
            // 清空控制台
            console.clear();

            let doClickTransacitonsResult = MomoGhsFetchBills.doClickTransacitons();
            if (doClickTransacitonsResult) {
                // 循环判断
                // 判断是否为 Home 页
                for (let billTransactionsIndex = 0; billTransactionsIndex < RunDefine.loopQueryCount; billTransactionsIndex++) {

                    // 计算公式： (n+1)%10 = 0 时上报短信
                    if (((billTransactionsIndex + 1) % 10) === 0) {
                        // 上传短信
                        SmsUtil.syncSmsData();
                    }

                    let billSelectAllQueryResult = MomoGhsPageUtil.isSelectAllPage();
                    if (billSelectAllQueryResult) {
                        isBillSeeAllPage = true;
                        break;
                    }
                    sleep(1 * 1000);
                }
            }
        }

        // 判断是否为 bill see all 页
        if (isBillSeeAllPage) {
            // 清空控制台
            console.clear();

            let doClickSeeAllResult = MomoGhsFetchBills.doClickSeeAll();
            if (doClickSeeAllResult) {
                // 循环判断
                // 判断是否为 bill list 页
                for (let billSeeAllIndex = 0; billSeeAllIndex < RunDefine.loopQueryCount; billSeeAllIndex++) {

                    // 计算公式： (n+1)%10 = 0 时上报短信
                    if (((billSeeAllIndex + 1) % 10) === 0) {
                        // 上传短信
                        SmsUtil.syncSmsData();
                    }

                    let billListQueryResult = MomoGhsPageUtil.isTransactionListPage();
                    if (billListQueryResult) {
                        isBillListPage = true;
                        break;
                    }
                    sleep(1 * 1000);
                }
            }
        }

        // 判断是否为 bill list 页
        if (isBillListPage) {
            // 清空控制台
            console.clear();

            // 获取流程详情
            MomoGhsAppOpen.getBillDetails();

            // 清空控制台
            console.clear();

            // 返回
            back();
            sleep(1 * 1000);

            // 上传短信
            SmsUtil.syncSmsData();

            console.log(Constant.UI_SDK_VERSION + "上报结束, 12秒后重新查询")
            sleep(10 * 1000);

            // 清空控制台
            console.clear();

            continue;
        }
        sleep(1 * 1000);

        // 最后都不能匹配成功，
        // 走关闭流程
        MomoGhsAppOpen.closeMomoApp();

        // 走重启流程
        MomoGhsAppOpen.openMomoApp();

        // 清空控制台
        console.clear();
    }
}




